package com.ezandroid.util;

import android.os.Environment;

import java.io.File;

/**
 * File util.
 *
 * @author markmjw
 * @date 2014-10-28
 */
public class FileUtil {

    public static final String EXTERNAL_STORAGE = Environment.getExternalStorageDirectory()
            .toString();

    private static String DIR_HOME = EXTERNAL_STORAGE + "/ezandroid";
    private static String DIR_IMAGE = DIR_HOME + "/image";
    private static String DIR_CACHE = DIR_HOME + "/cache";
    private static String DIR_LOG = DIR_HOME + "/log";
    private static String DIR_DOWNLOAD = DIR_HOME + "/download";
    private static final String DIR_NO_MEDIA_FILE = DIR_HOME + "/.nomedia";

    public static final int DIR_TYPE_CACHE = 0x01;
    public static final int DIR_TYPE_IMAGE = DIR_TYPE_CACHE << 1;
    public static final int DIR_TYPE_LOG = DIR_TYPE_IMAGE << 1;
    public static final int DIR_TYPE_DOWNLOAD = DIR_TYPE_LOG << 1;

    /**
     * 通过类型获取目录路径
     *
     * @param type
     * @return
     */
    public static String getPathByType(int type) {
        String dir = "/";
        String filePath;

        switch (type) {
            case DIR_TYPE_CACHE:
                filePath = DIR_CACHE;
                break;

            case DIR_TYPE_IMAGE:
                filePath = DIR_IMAGE;
                break;

            case DIR_TYPE_LOG:
                filePath = DIR_LOG;
                break;

            case DIR_TYPE_DOWNLOAD:
                filePath = DIR_DOWNLOAD;
                break;

            default:
                filePath = "";
                break;
        }

        File file = new File(filePath);
        if (!file.exists() || !file.isDirectory()) {
            file.mkdirs();
        }

        if (file.exists()) {
            if (file.isDirectory()) {
                dir = file.getPath();
            }
        } else {
            // 文件没创建成功，可能是sd卡不存在，但是还是把路径返回
            dir = filePath;
        }

        return dir + "/";
    }
}
